package com.woniu.service.impl;

import com.woniu.entity.Employee;
import com.woniu.mapper.EmployeeMapper;
import com.woniu.service.EmployeeService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public ResponseUtil delete(Integer employeeId) {
        int delete = employeeMapper.delete(employeeId);
        return delete>0?ResponseUtil.get(ResponseEnum.OK):ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 添加员工，增加账号唯一性检查
     */
    @Override
    public ResponseUtil add(Employee employee) {
        // 1. 检查账号是否已存在
        Employee existingEmployee = employeeMapper.findByUsername(employee.getUsername());
        if (existingEmployee != null) {
            return new ResponseUtil(ResponseEnum.FAIL.getCode(), "账号已存在,请更换");
        }

        // 2. 执行添加操作
        int add = employeeMapper.add(employee);
        return add > 0 ? ResponseUtil.get(ResponseEnum.OK) : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByEmployeeId(Integer employeeId) {
        Employee employee = employeeMapper.findByEmployeeId(employeeId);
        return employee != null ? ResponseUtil.get(ResponseEnum.OK, employee) : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil update(Employee employee) {
        int update = employeeMapper.update(employee);
        return update > 0 ? ResponseUtil.get(ResponseEnum.OK) : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findAll() {
        List<Employee> employeeList = employeeMapper.findAll();
        return !employeeList.isEmpty() ? ResponseUtil.get(ResponseEnum.OK, employeeList) : ResponseUtil.get(ResponseEnum.FAIL);
    }

    /**
     * 实现动态条件查询
     */
    @Override
    public ResponseUtil findWithConditions(String name, String gender, Integer deptId, Date beginDate, Date endDate) {
        List<Employee> employeeList = employeeMapper.findWithConditions(name, gender, deptId, beginDate, endDate);
        // 对于查询来说，即使结果为空列表，也认为是成功的操作
        return ResponseUtil.get(ResponseEnum.OK, employeeList);

    }
}